package org.example.com.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.com.Bean.Order;
import org.example.com.Bean.OrderItem;
import org.example.com.mapper.OrderMapper;
import org.example.com.service.OrderItemService;
import org.example.com.service.OrderService;
import org.example.com.util.JsonResult;
import org.example.com.util.RecultTool;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

   @Resource
   private OrderItemService orderItemService;
   @Resource
   private StringRedisTemplate stringRedisTemplate;
   @Override
    public JsonResult saveOrder(Order order, String[] ids) {
       HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
       save(order);
       for(int i=0; i<ids.length; i++) {
           OrderItem orderItem = new OrderItem(); 
           orderItem.setOrderId(order.getId());
           orderItem.setBookId(ids[i]);
           Object number=ops.get("gwc:"+order.getUserId(),ids[i]);
           if (number==null){
               orderItem.setNumber("1");
           }else{
               orderItem.setNumber(number.toString());
           }
           orderItemService.save(orderItem);
       }
       return RecultTool.success("success");
    }

}
